﻿#include "GUILoadBaseDialog.h"

#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"

#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/PickedData.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include <QComboBox>
#include <QMessageBox>
#include <QStandardItemModel>

namespace GUI {
    GUILoadBaseDialog::GUILoadBaseDialog(Acoustics::FITKAcousticsAbstractLoad* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUIDialogBase(parent),
        _obj(obj),
        _oper(oper)
    {
        if (_obj) {
            _isCreate = false;
        }
        else {
            _isCreate = true;
        }

        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
    }

    GUILoadBaseDialog::~GUILoadBaseDialog()
    {
        //销毁对象
    }

    void GUILoadBaseDialog::init()
    {
        //初始化，需要在基类重写
    }

    bool GUILoadBaseDialog::createObjData()
    {
        if (!_oper || !_obj) return false;
        //获取物理数据
        Acoustics::FITKAcousticsPhysics* phyMgr = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!phyMgr) return false;
        //获取载荷管理器
        Acoustics::FITKAcousticsLoadManager* loadMgr = phyMgr->getFITKAcousticsLoadManager();
        if (!loadMgr) return false;

        //获取板名称-查重处理
        QString name = this->getNameFormWidget();
        if (loadMgr->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
            return false;
        }
        //设置名称
        _obj->setDataObjectName(name);
        //设置界面数据
        this->getDataFormWidget();
        //设置数据
        loadMgr->appendDataObj(_obj);
        _oper->setArgs("objID", _obj->getDataObjectID());

        //清除拾取模式和高亮
        GUI::GUIPickInfo::ClearPickInfo();
        return true;
    }

    bool GUILoadBaseDialog::editObjData()
    {
        if (!_oper || !_obj) return false;
        //获取物理数据
        Acoustics::FITKAcousticsPhysics* phyMgr = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!phyMgr) return false;
        //获取载荷管理器
        Acoustics::FITKAcousticsLoadManager* loadMgr = phyMgr->getFITKAcousticsLoadManager();
        if (!loadMgr) return false;

        //获取板名称-查重处理
        QString name = this->getNameFormWidget();
        //名称重复判断
        if (name != _obj->getDataObjectName())
            if (loadMgr->getDataByName(name)) {
                QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
                return false;
            }
        //设置名称
        _obj->setDataObjectName(name);
        //设置界面数据
        this->getDataFormWidget();
        _oper->setArgs("objID", _obj->getDataObjectID());
        return true;
    }

    void GUILoadBaseDialog::setComboBoxSpectra(QList<QComboBox*> comboBoxList, QList<int> typeList, bool isSetDefault)
    {
        if (comboBoxList.isEmpty()) return;
        //清空数据下拉链表
        for (QComboBox* comboBox : comboBoxList)
        {
            if (!comboBox) continue;
            comboBox->clear();
            if (isSetDefault)
                comboBox->addItem("None", -1);
        }
        //初始化损耗因子下拉列表
        Acoustics::FITKAcousticsPhysics* phyMgr = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!phyMgr)return;
        //获取频谱管理器
        Acoustics::FITKAcousticsSpectraManager* spectraMgr = phyMgr->getFITKAcousticsSpectraManager();
        if (!spectraMgr)return;
        int count = spectraMgr->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            Acoustics::FITKAcousticsAbstractSpectra* spectraData = spectraMgr->getDataByIndex(i);
            if (!spectraData) continue;
            int type = spectraData->getSpectraType();
            if (!typeList.contains(type))continue;
            for (QComboBox* comboBox : comboBoxList)
            {
                if (!comboBox) continue;
                comboBox->addItem(spectraData->getDataObjectName(), spectraData->getDataObjectID());
            }
        }
    }

    bool GUILoadBaseDialog::getPickDiscDataType(int pickObjID, bool& isSEAModel, int& seaType)
    {
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel) return false;
        //获取FE模型和SEA模型
        Acoustics::FITKAcousticsFEModel* feModel = discModel->getFEModel();
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel || !feModel) return false;
        //获取子系统管理器
        Acoustics::FITKAcousticsSEASubSysManager* seaManager = seaModel->getSEASubSysManager();
        Acoustics::FITKAcousticsFESubSysManager* feManager = feModel->getFESubSysManager();
        if (!seaManager || !feManager) return false;
        Acoustics::FITKAbstractSEASubSys* seaSubsysObj = seaManager->getDataByID(pickObjID);
        if (seaSubsysObj)
        {
            Acoustics::FITKAbstractSEASubSys::SEASubSysType type = seaSubsysObj->getSEASubSysType();
            isSEAModel = true;
            seaType = type;
            return true;
        }
        Acoustics::FITKAbstractFESubSys* feSubsysObj = feManager->getDataByID(pickObjID);
        if (feSubsysObj)
        {
            isSEAModel = false;
            return true;
        }
        return false;
    }

    QString GUILoadBaseDialog::ckeckNameFromMgr(QString name)
    {
        //获取物理数据
        Acoustics::FITKAcousticsPhysics* phyMgr = FITKAPP->getGlobalData()->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
        if (!phyMgr) return name;
        //获取载荷管理器
        Acoustics::FITKAcousticsLoadManager* loadMgr = phyMgr->getFITKAcousticsLoadManager();
        if (!loadMgr) return name;
        return loadMgr->checkName(name);
    }

    void GUILoadBaseDialog::getDataFormWidget()
    {
        //从界面获取数据-基类实现
    }

    QString GUILoadBaseDialog::getNameFormWidget()
    {
        //从界面里获取设置的名称-基类实现
        return QString();
    }

    void GUILoadBaseDialog::showEvent(QShowEvent * event)
    {
        if (_obj && _isCreate == false) {
            GUIDialogBase::hightGraphObj(_obj->getDataObjectID());
        }
        GUIDialogBase::showEvent(event);
    }

    void GUILoadBaseDialog::disHight()
    {
        if(_obj && _isCreate == false) {
            disHightGraphObj(_obj->getDataObjectID());
        }
    }

    void GUILoadBaseDialog::accept()
    {
        disHight();
        GUIDialogBase::accept();
    }

    void GUILoadBaseDialog::reject()
    {
        //清除拾取模式和高亮
        GUI::GUIPickInfo::ClearPickInfo();
        //关闭窗口
        _oper->setArgs("objID", -1);
        QString operModel;
        if (_oper->argValue("OperModelType", operModel) && operModel == "Create")
        {
            delete _obj;
            _obj = nullptr;
        }

        disHight();
        GUIDialogBase::reject();
    }
}
